<?php

class Contacts extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('Contacts_model');
	}
	
	function index()
	{	
		$this->search();
	}

	function search() {
		$data['contact_types'] = $this->Contacts_model->get_contact_types(true);

		$this->load->view('inc.header.php');
		$this->load->view('contacts/search/search.php',$data);
		$this->load->view('contacts/search/_popups/new_person.php');
		$this->load->view('contacts/search/_popups/edit_person.php');
		$this->load->view('contacts/search/_popups/new_company.php');
		$this->load->view('contacts/search/_popups/edit_company.php');
		$this->load->view('inc.footer.php');
	}
	
	function reports() {
		$data = array();
		$this->load->view('inc.header.php');
		$this->load->view('contacts/reports',$data);
		$this->load->view('inc.footer.php');
	}
	
	function ajax_create_report() {
		
	}
	
	function ajax_search() {
		$search = json_decode($this->input->post('json'));
		$results = $this->Contacts_model->search($search);
		echo json_encode($results);
	}
	
	function ajax_get_new_contact_fields() {
		$data['companies'] = $this->Contacts_model->get_companies(true);
		$data['contact_types'] = $this->Contacts_model->get_contact_types(true);
		echo json_encode($data);
	}
	
	function ajax_save_new_person() {
		$person = json_decode($this->input->post('json'));
		echo $this->Contacts_model->save_new_person($person);
	}
	
	function ajax_get_contact_fields() {
		$result = array();
		$info = json_decode($this->input->post('json'));
		$result['contact_fields'] = $this->Contacts_model->get_contact_fields($info);
		if (isset($info->contact_company_id)) {
			$result['contact_types'] = $this->Contacts_model->get_contact_types(false,$info->contact_company_id);
		} else {
			$result['contact_types'] = array();
		}
		echo json_encode($result);
	}
	
	function ajax_get_contact_details() {
		$info = json_decode($this->input->post('json'));
		$contact_details = $this->Contacts_model->get_contact_details($info);
		echo json_encode($contact_details);
	}
	
	function ajax_save_new_company() {
		$company = json_decode($this->input->post('json'));
		echo $this->Contacts_model->save_new_company($company);
	}
	
}
